/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.cmc.service.merchant.service.persistence;

import com.cmc.service.merchant.NoSuchAgentEntryException;
import com.cmc.service.merchant.model.AgentEntry;
import com.cmc.service.merchant.model.impl.AgentEntryImpl;
import com.cmc.service.merchant.model.impl.AgentEntryModelImpl;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.bean.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ContactPersistence;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the agent entry service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author ntcuong1
 * @see AgentEntryPersistence
 * @see AgentEntryUtil
 * @generated
 */
public class AgentEntryPersistenceImpl extends BasePersistenceImpl<AgentEntry>
	implements AgentEntryPersistence {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never modify or reference this class directly. Always use {@link AgentEntryUtil} to access the agent entry persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
	 */
	public static final String FINDER_CLASS_NAME_ENTITY = AgentEntryImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List1";
	public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List2";
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_MERCHANT = new FinderPath(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
			AgentEntryModelImpl.FINDER_CACHE_ENABLED, AgentEntryImpl.class,
			FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByMerchant",
			new String[] {
				Long.class.getName(),
				
			"java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MERCHANT =
		new FinderPath(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
			AgentEntryModelImpl.FINDER_CACHE_ENABLED, AgentEntryImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByMerchant",
			new String[] { Long.class.getName() },
			AgentEntryModelImpl.MERCHANTID_COLUMN_BITMASK);
	public static final FinderPath FINDER_PATH_COUNT_BY_MERCHANT = new FinderPath(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
			AgentEntryModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByMerchant",
			new String[] { Long.class.getName() });
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_M_T = new FinderPath(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
			AgentEntryModelImpl.FINDER_CACHE_ENABLED, AgentEntryImpl.class,
			FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByM_T",
			new String[] {
				Long.class.getName(), String.class.getName(),
				
			"java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_M_T = new FinderPath(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
			AgentEntryModelImpl.FINDER_CACHE_ENABLED, AgentEntryImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByM_T",
			new String[] { Long.class.getName(), String.class.getName() },
			AgentEntryModelImpl.MERCHANTID_COLUMN_BITMASK |
			AgentEntryModelImpl.TYPE_COLUMN_BITMASK);
	public static final FinderPath FINDER_PATH_COUNT_BY_M_T = new FinderPath(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
			AgentEntryModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByM_T",
			new String[] { Long.class.getName(), String.class.getName() });
	public static final FinderPath FINDER_PATH_FETCH_BY_CODE = new FinderPath(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
			AgentEntryModelImpl.FINDER_CACHE_ENABLED, AgentEntryImpl.class,
			FINDER_CLASS_NAME_ENTITY, "fetchByCode",
			new String[] { String.class.getName() },
			AgentEntryModelImpl.CODE_COLUMN_BITMASK);
	public static final FinderPath FINDER_PATH_COUNT_BY_CODE = new FinderPath(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
			AgentEntryModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCode",
			new String[] { String.class.getName() });
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
			AgentEntryModelImpl.FINDER_CACHE_ENABLED, AgentEntryImpl.class,
			FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
			AgentEntryModelImpl.FINDER_CACHE_ENABLED, AgentEntryImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
			AgentEntryModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);

	/**
	 * Caches the agent entry in the entity cache if it is enabled.
	 *
	 * @param agentEntry the agent entry
	 */
	public void cacheResult(AgentEntry agentEntry) {
		EntityCacheUtil.putResult(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
			AgentEntryImpl.class, agentEntry.getPrimaryKey(), agentEntry);

		FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
			new Object[] { agentEntry.getCode() }, agentEntry);

		agentEntry.resetOriginalValues();
	}

	/**
	 * Caches the agent entries in the entity cache if it is enabled.
	 *
	 * @param agentEntries the agent entries
	 */
	public void cacheResult(List<AgentEntry> agentEntries) {
		for (AgentEntry agentEntry : agentEntries) {
			if (EntityCacheUtil.getResult(
						AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
						AgentEntryImpl.class, agentEntry.getPrimaryKey()) == null) {
				cacheResult(agentEntry);
			}
			else {
				agentEntry.resetOriginalValues();
			}
		}
	}

	/**
	 * Clears the cache for all agent entries.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache() {
		if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
			CacheRegistryUtil.clear(AgentEntryImpl.class.getName());
		}

		EntityCacheUtil.clearCache(AgentEntryImpl.class.getName());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	/**
	 * Clears the cache for the agent entry.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache(AgentEntry agentEntry) {
		EntityCacheUtil.removeResult(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
			AgentEntryImpl.class, agentEntry.getPrimaryKey());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

		clearUniqueFindersCache(agentEntry);
	}

	@Override
	public void clearCache(List<AgentEntry> agentEntries) {
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

		for (AgentEntry agentEntry : agentEntries) {
			EntityCacheUtil.removeResult(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
				AgentEntryImpl.class, agentEntry.getPrimaryKey());

			clearUniqueFindersCache(agentEntry);
		}
	}

	protected void clearUniqueFindersCache(AgentEntry agentEntry) {
		FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
			new Object[] { agentEntry.getCode() });
	}

	/**
	 * Creates a new agent entry with the primary key. Does not add the agent entry to the database.
	 *
	 * @param agentId the primary key for the new agent entry
	 * @return the new agent entry
	 */
	public AgentEntry create(long agentId) {
		AgentEntry agentEntry = new AgentEntryImpl();

		agentEntry.setNew(true);
		agentEntry.setPrimaryKey(agentId);

		return agentEntry;
	}

	/**
	 * Removes the agent entry with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param agentId the primary key of the agent entry
	 * @return the agent entry that was removed
	 * @throws com.cmc.service.merchant.NoSuchAgentEntryException if a agent entry with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry remove(long agentId)
		throws NoSuchAgentEntryException, SystemException {
		return remove(Long.valueOf(agentId));
	}

	/**
	 * Removes the agent entry with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the agent entry
	 * @return the agent entry that was removed
	 * @throws com.cmc.service.merchant.NoSuchAgentEntryException if a agent entry with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public AgentEntry remove(Serializable primaryKey)
		throws NoSuchAgentEntryException, SystemException {
		Session session = null;

		try {
			session = openSession();

			AgentEntry agentEntry = (AgentEntry)session.get(AgentEntryImpl.class,
					primaryKey);

			if (agentEntry == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
				}

				throw new NoSuchAgentEntryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					primaryKey);
			}

			return remove(agentEntry);
		}
		catch (NoSuchAgentEntryException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	@Override
	protected AgentEntry removeImpl(AgentEntry agentEntry)
		throws SystemException {
		agentEntry = toUnwrappedModel(agentEntry);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, agentEntry);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		clearCache(agentEntry);

		return agentEntry;
	}

	@Override
	public AgentEntry updateImpl(
		com.cmc.service.merchant.model.AgentEntry agentEntry, boolean merge)
		throws SystemException {
		agentEntry = toUnwrappedModel(agentEntry);

		boolean isNew = agentEntry.isNew();

		AgentEntryModelImpl agentEntryModelImpl = (AgentEntryModelImpl)agentEntry;

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, agentEntry, merge);

			agentEntry.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

		if (isNew || !AgentEntryModelImpl.COLUMN_BITMASK_ENABLED) {
			FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
		}

		else {
			if ((agentEntryModelImpl.getColumnBitmask() &
					FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MERCHANT.getColumnBitmask()) != 0) {
				Object[] args = new Object[] {
						Long.valueOf(agentEntryModelImpl.getOriginalMerchantId())
					};

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_MERCHANT, args);
				FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MERCHANT,
					args);

				args = new Object[] {
						Long.valueOf(agentEntryModelImpl.getMerchantId())
					};

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_MERCHANT, args);
				FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MERCHANT,
					args);
			}

			if ((agentEntryModelImpl.getColumnBitmask() &
					FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_M_T.getColumnBitmask()) != 0) {
				Object[] args = new Object[] {
						Long.valueOf(agentEntryModelImpl.getOriginalMerchantId()),
						
						agentEntryModelImpl.getOriginalType()
					};

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_M_T, args);
				FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_M_T,
					args);

				args = new Object[] {
						Long.valueOf(agentEntryModelImpl.getMerchantId()),
						
						agentEntryModelImpl.getType()
					};

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_M_T, args);
				FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_M_T,
					args);
			}
		}

		EntityCacheUtil.putResult(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
			AgentEntryImpl.class, agentEntry.getPrimaryKey(), agentEntry);

		if (isNew) {
			FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
				new Object[] { agentEntry.getCode() }, agentEntry);
		}
		else {
			if ((agentEntryModelImpl.getColumnBitmask() &
					FINDER_PATH_FETCH_BY_CODE.getColumnBitmask()) != 0) {
				Object[] args = new Object[] {
						agentEntryModelImpl.getOriginalCode()
					};

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CODE, args);

				FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE, args);

				FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
					new Object[] { agentEntry.getCode() }, agentEntry);
			}
		}

		return agentEntry;
	}

	protected AgentEntry toUnwrappedModel(AgentEntry agentEntry) {
		if (agentEntry instanceof AgentEntryImpl) {
			return agentEntry;
		}

		AgentEntryImpl agentEntryImpl = new AgentEntryImpl();

		agentEntryImpl.setNew(agentEntry.isNew());
		agentEntryImpl.setPrimaryKey(agentEntry.getPrimaryKey());

		agentEntryImpl.setAgentId(agentEntry.getAgentId());
		agentEntryImpl.setCreatorUserId(agentEntry.getCreatorUserId());
		agentEntryImpl.setCompanyId(agentEntry.getCompanyId());
		agentEntryImpl.setCreateDate(agentEntry.getCreateDate());
		agentEntryImpl.setModifiedDate(agentEntry.getModifiedDate());
		agentEntryImpl.setType(agentEntry.getType());
		agentEntryImpl.setCode(agentEntry.getCode());
		agentEntryImpl.setFirstName(agentEntry.getFirstName());
		agentEntryImpl.setMiddleName(agentEntry.getMiddleName());
		agentEntryImpl.setLastName(agentEntry.getLastName());
		agentEntryImpl.setPrefixId(agentEntry.getPrefixId());
		agentEntryImpl.setSuffixId(agentEntry.getSuffixId());
		agentEntryImpl.setMale(agentEntry.isMale());
		agentEntryImpl.setBirthdayMonth(agentEntry.getBirthdayMonth());
		agentEntryImpl.setBirthdayYear(agentEntry.getBirthdayYear());
		agentEntryImpl.setBirthdayDay(agentEntry.getBirthdayDay());
		agentEntryImpl.setJobTitle(agentEntry.getJobTitle());
		agentEntryImpl.setEmail(agentEntry.getEmail());
		agentEntryImpl.setMerchantId(agentEntry.getMerchantId());
		agentEntryImpl.setLoginable(agentEntry.isLoginable());
		agentEntryImpl.setUserId(agentEntry.getUserId());
		agentEntryImpl.setScreenName(agentEntry.getScreenName());

		return agentEntryImpl;
	}

	/**
	 * Returns the agent entry with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the agent entry
	 * @return the agent entry
	 * @throws com.liferay.portal.NoSuchModelException if a agent entry with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public AgentEntry findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Returns the agent entry with the primary key or throws a {@link com.cmc.service.merchant.NoSuchAgentEntryException} if it could not be found.
	 *
	 * @param agentId the primary key of the agent entry
	 * @return the agent entry
	 * @throws com.cmc.service.merchant.NoSuchAgentEntryException if a agent entry with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry findByPrimaryKey(long agentId)
		throws NoSuchAgentEntryException, SystemException {
		AgentEntry agentEntry = fetchByPrimaryKey(agentId);

		if (agentEntry == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + agentId);
			}

			throw new NoSuchAgentEntryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				agentId);
		}

		return agentEntry;
	}

	/**
	 * Returns the agent entry with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the agent entry
	 * @return the agent entry, or <code>null</code> if a agent entry with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public AgentEntry fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Returns the agent entry with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param agentId the primary key of the agent entry
	 * @return the agent entry, or <code>null</code> if a agent entry with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry fetchByPrimaryKey(long agentId) throws SystemException {
		AgentEntry agentEntry = (AgentEntry)EntityCacheUtil.getResult(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
				AgentEntryImpl.class, agentId);

		if (agentEntry == _nullAgentEntry) {
			return null;
		}

		if (agentEntry == null) {
			Session session = null;

			boolean hasException = false;

			try {
				session = openSession();

				agentEntry = (AgentEntry)session.get(AgentEntryImpl.class,
						Long.valueOf(agentId));
			}
			catch (Exception e) {
				hasException = true;

				throw processException(e);
			}
			finally {
				if (agentEntry != null) {
					cacheResult(agentEntry);
				}
				else if (!hasException) {
					EntityCacheUtil.putResult(AgentEntryModelImpl.ENTITY_CACHE_ENABLED,
						AgentEntryImpl.class, agentId, _nullAgentEntry);
				}

				closeSession(session);
			}
		}

		return agentEntry;
	}

	/**
	 * Returns all the agent entries where merchantId = &#63;.
	 *
	 * @param merchantId the merchant ID
	 * @return the matching agent entries
	 * @throws SystemException if a system exception occurred
	 */
	public List<AgentEntry> findByMerchant(long merchantId)
		throws SystemException {
		return findByMerchant(merchantId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
			null);
	}

	/**
	 * Returns a range of all the agent entries where merchantId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param merchantId the merchant ID
	 * @param start the lower bound of the range of agent entries
	 * @param end the upper bound of the range of agent entries (not inclusive)
	 * @return the range of matching agent entries
	 * @throws SystemException if a system exception occurred
	 */
	public List<AgentEntry> findByMerchant(long merchantId, int start, int end)
		throws SystemException {
		return findByMerchant(merchantId, start, end, null);
	}

	/**
	 * Returns an ordered range of all the agent entries where merchantId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param merchantId the merchant ID
	 * @param start the lower bound of the range of agent entries
	 * @param end the upper bound of the range of agent entries (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of matching agent entries
	 * @throws SystemException if a system exception occurred
	 */
	public List<AgentEntry> findByMerchant(long merchantId, int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		FinderPath finderPath = null;
		Object[] finderArgs = null;

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_MERCHANT;
			finderArgs = new Object[] { merchantId };
		}
		else {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_MERCHANT;
			finderArgs = new Object[] { merchantId, start, end, orderByComparator };
		}

		List<AgentEntry> list = (List<AgentEntry>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if ((list != null) && !list.isEmpty()) {
			for (AgentEntry agentEntry : list) {
				if ((merchantId != agentEntry.getMerchantId())) {
					list = null;

					break;
				}
			}
		}

		if (list == null) {
			StringBundler query = null;

			if (orderByComparator != null) {
				query = new StringBundler(3 +
						(orderByComparator.getOrderByFields().length * 3));
			}
			else {
				query = new StringBundler(3);
			}

			query.append(_SQL_SELECT_AGENTENTRY_WHERE);

			query.append(_FINDER_COLUMN_MERCHANT_MERCHANTID_2);

			if (orderByComparator != null) {
				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);
			}

			else {
				query.append(AgentEntryModelImpl.ORDER_BY_JPQL);
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(merchantId);

				list = (List<AgentEntry>)QueryUtil.list(q, getDialect(), start,
						end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(finderPath, finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(finderPath, finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Returns the first agent entry in the ordered set where merchantId = &#63;.
	 *
	 * @param merchantId the merchant ID
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the first matching agent entry
	 * @throws com.cmc.service.merchant.NoSuchAgentEntryException if a matching agent entry could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry findByMerchant_First(long merchantId,
		OrderByComparator orderByComparator)
		throws NoSuchAgentEntryException, SystemException {
		AgentEntry agentEntry = fetchByMerchant_First(merchantId,
				orderByComparator);

		if (agentEntry != null) {
			return agentEntry;
		}

		StringBundler msg = new StringBundler(4);

		msg.append(_NO_SUCH_ENTITY_WITH_KEY);

		msg.append("merchantId=");
		msg.append(merchantId);

		msg.append(StringPool.CLOSE_CURLY_BRACE);

		throw new NoSuchAgentEntryException(msg.toString());
	}

	/**
	 * Returns the first agent entry in the ordered set where merchantId = &#63;.
	 *
	 * @param merchantId the merchant ID
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the first matching agent entry, or <code>null</code> if a matching agent entry could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry fetchByMerchant_First(long merchantId,
		OrderByComparator orderByComparator) throws SystemException {
		List<AgentEntry> list = findByMerchant(merchantId, 0, 1,
				orderByComparator);

		if (!list.isEmpty()) {
			return list.get(0);
		}

		return null;
	}

	/**
	 * Returns the last agent entry in the ordered set where merchantId = &#63;.
	 *
	 * @param merchantId the merchant ID
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the last matching agent entry
	 * @throws com.cmc.service.merchant.NoSuchAgentEntryException if a matching agent entry could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry findByMerchant_Last(long merchantId,
		OrderByComparator orderByComparator)
		throws NoSuchAgentEntryException, SystemException {
		AgentEntry agentEntry = fetchByMerchant_Last(merchantId,
				orderByComparator);

		if (agentEntry != null) {
			return agentEntry;
		}

		StringBundler msg = new StringBundler(4);

		msg.append(_NO_SUCH_ENTITY_WITH_KEY);

		msg.append("merchantId=");
		msg.append(merchantId);

		msg.append(StringPool.CLOSE_CURLY_BRACE);

		throw new NoSuchAgentEntryException(msg.toString());
	}

	/**
	 * Returns the last agent entry in the ordered set where merchantId = &#63;.
	 *
	 * @param merchantId the merchant ID
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the last matching agent entry, or <code>null</code> if a matching agent entry could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry fetchByMerchant_Last(long merchantId,
		OrderByComparator orderByComparator) throws SystemException {
		int count = countByMerchant(merchantId);

		List<AgentEntry> list = findByMerchant(merchantId, count - 1, count,
				orderByComparator);

		if (!list.isEmpty()) {
			return list.get(0);
		}

		return null;
	}

	/**
	 * Returns the agent entries before and after the current agent entry in the ordered set where merchantId = &#63;.
	 *
	 * @param agentId the primary key of the current agent entry
	 * @param merchantId the merchant ID
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the previous, current, and next agent entry
	 * @throws com.cmc.service.merchant.NoSuchAgentEntryException if a agent entry with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry[] findByMerchant_PrevAndNext(long agentId,
		long merchantId, OrderByComparator orderByComparator)
		throws NoSuchAgentEntryException, SystemException {
		AgentEntry agentEntry = findByPrimaryKey(agentId);

		Session session = null;

		try {
			session = openSession();

			AgentEntry[] array = new AgentEntryImpl[3];

			array[0] = getByMerchant_PrevAndNext(session, agentEntry,
					merchantId, orderByComparator, true);

			array[1] = agentEntry;

			array[2] = getByMerchant_PrevAndNext(session, agentEntry,
					merchantId, orderByComparator, false);

			return array;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected AgentEntry getByMerchant_PrevAndNext(Session session,
		AgentEntry agentEntry, long merchantId,
		OrderByComparator orderByComparator, boolean previous) {
		StringBundler query = null;

		if (orderByComparator != null) {
			query = new StringBundler(6 +
					(orderByComparator.getOrderByFields().length * 6));
		}
		else {
			query = new StringBundler(3);
		}

		query.append(_SQL_SELECT_AGENTENTRY_WHERE);

		query.append(_FINDER_COLUMN_MERCHANT_MERCHANTID_2);

		if (orderByComparator != null) {
			String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

			if (orderByConditionFields.length > 0) {
				query.append(WHERE_AND);
			}

			for (int i = 0; i < orderByConditionFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByConditionFields[i]);

				if ((i + 1) < orderByConditionFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN_HAS_NEXT);
					}
					else {
						query.append(WHERE_LESSER_THAN_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN);
					}
					else {
						query.append(WHERE_LESSER_THAN);
					}
				}
			}

			query.append(ORDER_BY_CLAUSE);

			String[] orderByFields = orderByComparator.getOrderByFields();

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC_HAS_NEXT);
					}
					else {
						query.append(ORDER_BY_DESC_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC);
					}
					else {
						query.append(ORDER_BY_DESC);
					}
				}
			}
		}

		else {
			query.append(AgentEntryModelImpl.ORDER_BY_JPQL);
		}

		String sql = query.toString();

		Query q = session.createQuery(sql);

		q.setFirstResult(0);
		q.setMaxResults(2);

		QueryPos qPos = QueryPos.getInstance(q);

		qPos.add(merchantId);

		if (orderByComparator != null) {
			Object[] values = orderByComparator.getOrderByConditionValues(agentEntry);

			for (Object value : values) {
				qPos.add(value);
			}
		}

		List<AgentEntry> list = q.list();

		if (list.size() == 2) {
			return list.get(1);
		}
		else {
			return null;
		}
	}

	/**
	 * Returns all the agent entries where merchantId = &#63; and type = &#63;.
	 *
	 * @param merchantId the merchant ID
	 * @param type the type
	 * @return the matching agent entries
	 * @throws SystemException if a system exception occurred
	 */
	public List<AgentEntry> findByM_T(long merchantId, String type)
		throws SystemException {
		return findByM_T(merchantId, type, QueryUtil.ALL_POS,
			QueryUtil.ALL_POS, null);
	}

	/**
	 * Returns a range of all the agent entries where merchantId = &#63; and type = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param merchantId the merchant ID
	 * @param type the type
	 * @param start the lower bound of the range of agent entries
	 * @param end the upper bound of the range of agent entries (not inclusive)
	 * @return the range of matching agent entries
	 * @throws SystemException if a system exception occurred
	 */
	public List<AgentEntry> findByM_T(long merchantId, String type, int start,
		int end) throws SystemException {
		return findByM_T(merchantId, type, start, end, null);
	}

	/**
	 * Returns an ordered range of all the agent entries where merchantId = &#63; and type = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param merchantId the merchant ID
	 * @param type the type
	 * @param start the lower bound of the range of agent entries
	 * @param end the upper bound of the range of agent entries (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of matching agent entries
	 * @throws SystemException if a system exception occurred
	 */
	public List<AgentEntry> findByM_T(long merchantId, String type, int start,
		int end, OrderByComparator orderByComparator) throws SystemException {
		FinderPath finderPath = null;
		Object[] finderArgs = null;

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_M_T;
			finderArgs = new Object[] { merchantId, type };
		}
		else {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_M_T;
			finderArgs = new Object[] {
					merchantId, type,
					
					start, end, orderByComparator
				};
		}

		List<AgentEntry> list = (List<AgentEntry>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if ((list != null) && !list.isEmpty()) {
			for (AgentEntry agentEntry : list) {
				if ((merchantId != agentEntry.getMerchantId()) ||
						!Validator.equals(type, agentEntry.getType())) {
					list = null;

					break;
				}
			}
		}

		if (list == null) {
			StringBundler query = null;

			if (orderByComparator != null) {
				query = new StringBundler(4 +
						(orderByComparator.getOrderByFields().length * 3));
			}
			else {
				query = new StringBundler(4);
			}

			query.append(_SQL_SELECT_AGENTENTRY_WHERE);

			query.append(_FINDER_COLUMN_M_T_MERCHANTID_2);

			if (type == null) {
				query.append(_FINDER_COLUMN_M_T_TYPE_1);
			}
			else {
				if (type.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_M_T_TYPE_3);
				}
				else {
					query.append(_FINDER_COLUMN_M_T_TYPE_2);
				}
			}

			if (orderByComparator != null) {
				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);
			}

			else {
				query.append(AgentEntryModelImpl.ORDER_BY_JPQL);
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(merchantId);

				if (type != null) {
					qPos.add(type);
				}

				list = (List<AgentEntry>)QueryUtil.list(q, getDialect(), start,
						end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(finderPath, finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(finderPath, finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Returns the first agent entry in the ordered set where merchantId = &#63; and type = &#63;.
	 *
	 * @param merchantId the merchant ID
	 * @param type the type
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the first matching agent entry
	 * @throws com.cmc.service.merchant.NoSuchAgentEntryException if a matching agent entry could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry findByM_T_First(long merchantId, String type,
		OrderByComparator orderByComparator)
		throws NoSuchAgentEntryException, SystemException {
		AgentEntry agentEntry = fetchByM_T_First(merchantId, type,
				orderByComparator);

		if (agentEntry != null) {
			return agentEntry;
		}

		StringBundler msg = new StringBundler(6);

		msg.append(_NO_SUCH_ENTITY_WITH_KEY);

		msg.append("merchantId=");
		msg.append(merchantId);

		msg.append(", type=");
		msg.append(type);

		msg.append(StringPool.CLOSE_CURLY_BRACE);

		throw new NoSuchAgentEntryException(msg.toString());
	}

	/**
	 * Returns the first agent entry in the ordered set where merchantId = &#63; and type = &#63;.
	 *
	 * @param merchantId the merchant ID
	 * @param type the type
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the first matching agent entry, or <code>null</code> if a matching agent entry could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry fetchByM_T_First(long merchantId, String type,
		OrderByComparator orderByComparator) throws SystemException {
		List<AgentEntry> list = findByM_T(merchantId, type, 0, 1,
				orderByComparator);

		if (!list.isEmpty()) {
			return list.get(0);
		}

		return null;
	}

	/**
	 * Returns the last agent entry in the ordered set where merchantId = &#63; and type = &#63;.
	 *
	 * @param merchantId the merchant ID
	 * @param type the type
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the last matching agent entry
	 * @throws com.cmc.service.merchant.NoSuchAgentEntryException if a matching agent entry could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry findByM_T_Last(long merchantId, String type,
		OrderByComparator orderByComparator)
		throws NoSuchAgentEntryException, SystemException {
		AgentEntry agentEntry = fetchByM_T_Last(merchantId, type,
				orderByComparator);

		if (agentEntry != null) {
			return agentEntry;
		}

		StringBundler msg = new StringBundler(6);

		msg.append(_NO_SUCH_ENTITY_WITH_KEY);

		msg.append("merchantId=");
		msg.append(merchantId);

		msg.append(", type=");
		msg.append(type);

		msg.append(StringPool.CLOSE_CURLY_BRACE);

		throw new NoSuchAgentEntryException(msg.toString());
	}

	/**
	 * Returns the last agent entry in the ordered set where merchantId = &#63; and type = &#63;.
	 *
	 * @param merchantId the merchant ID
	 * @param type the type
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the last matching agent entry, or <code>null</code> if a matching agent entry could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry fetchByM_T_Last(long merchantId, String type,
		OrderByComparator orderByComparator) throws SystemException {
		int count = countByM_T(merchantId, type);

		List<AgentEntry> list = findByM_T(merchantId, type, count - 1, count,
				orderByComparator);

		if (!list.isEmpty()) {
			return list.get(0);
		}

		return null;
	}

	/**
	 * Returns the agent entries before and after the current agent entry in the ordered set where merchantId = &#63; and type = &#63;.
	 *
	 * @param agentId the primary key of the current agent entry
	 * @param merchantId the merchant ID
	 * @param type the type
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the previous, current, and next agent entry
	 * @throws com.cmc.service.merchant.NoSuchAgentEntryException if a agent entry with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry[] findByM_T_PrevAndNext(long agentId, long merchantId,
		String type, OrderByComparator orderByComparator)
		throws NoSuchAgentEntryException, SystemException {
		AgentEntry agentEntry = findByPrimaryKey(agentId);

		Session session = null;

		try {
			session = openSession();

			AgentEntry[] array = new AgentEntryImpl[3];

			array[0] = getByM_T_PrevAndNext(session, agentEntry, merchantId,
					type, orderByComparator, true);

			array[1] = agentEntry;

			array[2] = getByM_T_PrevAndNext(session, agentEntry, merchantId,
					type, orderByComparator, false);

			return array;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected AgentEntry getByM_T_PrevAndNext(Session session,
		AgentEntry agentEntry, long merchantId, String type,
		OrderByComparator orderByComparator, boolean previous) {
		StringBundler query = null;

		if (orderByComparator != null) {
			query = new StringBundler(6 +
					(orderByComparator.getOrderByFields().length * 6));
		}
		else {
			query = new StringBundler(3);
		}

		query.append(_SQL_SELECT_AGENTENTRY_WHERE);

		query.append(_FINDER_COLUMN_M_T_MERCHANTID_2);

		if (type == null) {
			query.append(_FINDER_COLUMN_M_T_TYPE_1);
		}
		else {
			if (type.equals(StringPool.BLANK)) {
				query.append(_FINDER_COLUMN_M_T_TYPE_3);
			}
			else {
				query.append(_FINDER_COLUMN_M_T_TYPE_2);
			}
		}

		if (orderByComparator != null) {
			String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

			if (orderByConditionFields.length > 0) {
				query.append(WHERE_AND);
			}

			for (int i = 0; i < orderByConditionFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByConditionFields[i]);

				if ((i + 1) < orderByConditionFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN_HAS_NEXT);
					}
					else {
						query.append(WHERE_LESSER_THAN_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN);
					}
					else {
						query.append(WHERE_LESSER_THAN);
					}
				}
			}

			query.append(ORDER_BY_CLAUSE);

			String[] orderByFields = orderByComparator.getOrderByFields();

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC_HAS_NEXT);
					}
					else {
						query.append(ORDER_BY_DESC_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC);
					}
					else {
						query.append(ORDER_BY_DESC);
					}
				}
			}
		}

		else {
			query.append(AgentEntryModelImpl.ORDER_BY_JPQL);
		}

		String sql = query.toString();

		Query q = session.createQuery(sql);

		q.setFirstResult(0);
		q.setMaxResults(2);

		QueryPos qPos = QueryPos.getInstance(q);

		qPos.add(merchantId);

		if (type != null) {
			qPos.add(type);
		}

		if (orderByComparator != null) {
			Object[] values = orderByComparator.getOrderByConditionValues(agentEntry);

			for (Object value : values) {
				qPos.add(value);
			}
		}

		List<AgentEntry> list = q.list();

		if (list.size() == 2) {
			return list.get(1);
		}
		else {
			return null;
		}
	}

	/**
	 * Returns the agent entry where code = &#63; or throws a {@link com.cmc.service.merchant.NoSuchAgentEntryException} if it could not be found.
	 *
	 * @param code the code
	 * @return the matching agent entry
	 * @throws com.cmc.service.merchant.NoSuchAgentEntryException if a matching agent entry could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry findByCode(String code)
		throws NoSuchAgentEntryException, SystemException {
		AgentEntry agentEntry = fetchByCode(code);

		if (agentEntry == null) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("code=");
			msg.append(code);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			if (_log.isWarnEnabled()) {
				_log.warn(msg.toString());
			}

			throw new NoSuchAgentEntryException(msg.toString());
		}

		return agentEntry;
	}

	/**
	 * Returns the agent entry where code = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
	 *
	 * @param code the code
	 * @return the matching agent entry, or <code>null</code> if a matching agent entry could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry fetchByCode(String code) throws SystemException {
		return fetchByCode(code, true);
	}

	/**
	 * Returns the agent entry where code = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
	 *
	 * @param code the code
	 * @param retrieveFromCache whether to use the finder cache
	 * @return the matching agent entry, or <code>null</code> if a matching agent entry could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry fetchByCode(String code, boolean retrieveFromCache)
		throws SystemException {
		Object[] finderArgs = new Object[] { code };

		Object result = null;

		if (retrieveFromCache) {
			result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_CODE,
					finderArgs, this);
		}

		if (result instanceof AgentEntry) {
			AgentEntry agentEntry = (AgentEntry)result;

			if (!Validator.equals(code, agentEntry.getCode())) {
				result = null;
			}
		}

		if (result == null) {
			StringBundler query = new StringBundler(3);

			query.append(_SQL_SELECT_AGENTENTRY_WHERE);

			if (code == null) {
				query.append(_FINDER_COLUMN_CODE_CODE_1);
			}
			else {
				if (code.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_CODE_CODE_3);
				}
				else {
					query.append(_FINDER_COLUMN_CODE_CODE_2);
				}
			}

			query.append(AgentEntryModelImpl.ORDER_BY_JPQL);

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (code != null) {
					qPos.add(code);
				}

				List<AgentEntry> list = q.list();

				result = list;

				AgentEntry agentEntry = null;

				if (list.isEmpty()) {
					FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
						finderArgs, list);
				}
				else {
					agentEntry = list.get(0);

					cacheResult(agentEntry);

					if ((agentEntry.getCode() == null) ||
							!agentEntry.getCode().equals(code)) {
						FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_CODE,
							finderArgs, agentEntry);
					}
				}

				return agentEntry;
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (result == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_CODE,
						finderArgs);
				}

				closeSession(session);
			}
		}
		else {
			if (result instanceof List<?>) {
				return null;
			}
			else {
				return (AgentEntry)result;
			}
		}
	}

	/**
	 * Returns all the agent entries.
	 *
	 * @return the agent entries
	 * @throws SystemException if a system exception occurred
	 */
	public List<AgentEntry> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Returns a range of all the agent entries.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of agent entries
	 * @param end the upper bound of the range of agent entries (not inclusive)
	 * @return the range of agent entries
	 * @throws SystemException if a system exception occurred
	 */
	public List<AgentEntry> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Returns an ordered range of all the agent entries.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of agent entries
	 * @param end the upper bound of the range of agent entries (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of agent entries
	 * @throws SystemException if a system exception occurred
	 */
	public List<AgentEntry> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		FinderPath finderPath = null;
		Object[] finderArgs = new Object[] { start, end, orderByComparator };

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
			finderArgs = FINDER_ARGS_EMPTY;
		}
		else {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
			finderArgs = new Object[] { start, end, orderByComparator };
		}

		List<AgentEntry> list = (List<AgentEntry>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_AGENTENTRY);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_AGENTENTRY.concat(AgentEntryModelImpl.ORDER_BY_JPQL);
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<AgentEntry>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<AgentEntry>)QueryUtil.list(q, getDialect(),
							start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(finderPath, finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(finderPath, finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the agent entries where merchantId = &#63; from the database.
	 *
	 * @param merchantId the merchant ID
	 * @throws SystemException if a system exception occurred
	 */
	public void removeByMerchant(long merchantId) throws SystemException {
		for (AgentEntry agentEntry : findByMerchant(merchantId)) {
			remove(agentEntry);
		}
	}

	/**
	 * Removes all the agent entries where merchantId = &#63; and type = &#63; from the database.
	 *
	 * @param merchantId the merchant ID
	 * @param type the type
	 * @throws SystemException if a system exception occurred
	 */
	public void removeByM_T(long merchantId, String type)
		throws SystemException {
		for (AgentEntry agentEntry : findByM_T(merchantId, type)) {
			remove(agentEntry);
		}
	}

	/**
	 * Removes the agent entry where code = &#63; from the database.
	 *
	 * @param code the code
	 * @return the agent entry that was removed
	 * @throws SystemException if a system exception occurred
	 */
	public AgentEntry removeByCode(String code)
		throws NoSuchAgentEntryException, SystemException {
		AgentEntry agentEntry = findByCode(code);

		return remove(agentEntry);
	}

	/**
	 * Removes all the agent entries from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (AgentEntry agentEntry : findAll()) {
			remove(agentEntry);
		}
	}

	/**
	 * Returns the number of agent entries where merchantId = &#63;.
	 *
	 * @param merchantId the merchant ID
	 * @return the number of matching agent entries
	 * @throws SystemException if a system exception occurred
	 */
	public int countByMerchant(long merchantId) throws SystemException {
		Object[] finderArgs = new Object[] { merchantId };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_MERCHANT,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_COUNT_AGENTENTRY_WHERE);

			query.append(_FINDER_COLUMN_MERCHANT_MERCHANTID_2);

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(merchantId);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_MERCHANT,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Returns the number of agent entries where merchantId = &#63; and type = &#63;.
	 *
	 * @param merchantId the merchant ID
	 * @param type the type
	 * @return the number of matching agent entries
	 * @throws SystemException if a system exception occurred
	 */
	public int countByM_T(long merchantId, String type)
		throws SystemException {
		Object[] finderArgs = new Object[] { merchantId, type };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_M_T,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(3);

			query.append(_SQL_COUNT_AGENTENTRY_WHERE);

			query.append(_FINDER_COLUMN_M_T_MERCHANTID_2);

			if (type == null) {
				query.append(_FINDER_COLUMN_M_T_TYPE_1);
			}
			else {
				if (type.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_M_T_TYPE_3);
				}
				else {
					query.append(_FINDER_COLUMN_M_T_TYPE_2);
				}
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(merchantId);

				if (type != null) {
					qPos.add(type);
				}

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_M_T, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Returns the number of agent entries where code = &#63;.
	 *
	 * @param code the code
	 * @return the number of matching agent entries
	 * @throws SystemException if a system exception occurred
	 */
	public int countByCode(String code) throws SystemException {
		Object[] finderArgs = new Object[] { code };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODE,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_COUNT_AGENTENTRY_WHERE);

			if (code == null) {
				query.append(_FINDER_COLUMN_CODE_CODE_1);
			}
			else {
				if (code.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_CODE_CODE_3);
				}
				else {
					query.append(_FINDER_COLUMN_CODE_CODE_2);
				}
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (code != null) {
					qPos.add(code);
				}

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODE,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Returns the number of agent entries.
	 *
	 * @return the number of agent entries
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				FINDER_ARGS_EMPTY, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_AGENTENTRY);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
					FINDER_ARGS_EMPTY, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the agent entry persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.cmc.service.merchant.model.AgentEntry")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<AgentEntry>> listenersList = new ArrayList<ModelListener<AgentEntry>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<AgentEntry>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	public void destroy() {
		EntityCacheUtil.removeCache(AgentEntryImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	@BeanReference(type = AgentEntryPersistence.class)
	protected AgentEntryPersistence agentEntryPersistence;
	@BeanReference(type = MerchantEntryPersistence.class)
	protected MerchantEntryPersistence merchantEntryPersistence;
	@BeanReference(type = ContactPersistence.class)
	protected ContactPersistence contactPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_AGENTENTRY = "SELECT agentEntry FROM AgentEntry agentEntry";
	private static final String _SQL_SELECT_AGENTENTRY_WHERE = "SELECT agentEntry FROM AgentEntry agentEntry WHERE ";
	private static final String _SQL_COUNT_AGENTENTRY = "SELECT COUNT(agentEntry) FROM AgentEntry agentEntry";
	private static final String _SQL_COUNT_AGENTENTRY_WHERE = "SELECT COUNT(agentEntry) FROM AgentEntry agentEntry WHERE ";
	private static final String _FINDER_COLUMN_MERCHANT_MERCHANTID_2 = "agentEntry.merchantId = ?";
	private static final String _FINDER_COLUMN_M_T_MERCHANTID_2 = "agentEntry.merchantId = ? AND ";
	private static final String _FINDER_COLUMN_M_T_TYPE_1 = "agentEntry.type IS NULL";
	private static final String _FINDER_COLUMN_M_T_TYPE_2 = "agentEntry.type = ?";
	private static final String _FINDER_COLUMN_M_T_TYPE_3 = "(agentEntry.type IS NULL OR agentEntry.type = ?)";
	private static final String _FINDER_COLUMN_CODE_CODE_1 = "agentEntry.code IS NULL";
	private static final String _FINDER_COLUMN_CODE_CODE_2 = "agentEntry.code = ?";
	private static final String _FINDER_COLUMN_CODE_CODE_3 = "(agentEntry.code IS NULL OR agentEntry.code = ?)";
	private static final String _ORDER_BY_ENTITY_ALIAS = "agentEntry.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No AgentEntry exists with the primary key ";
	private static final String _NO_SUCH_ENTITY_WITH_KEY = "No AgentEntry exists with the key {";
	private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
				PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
	private static Log _log = LogFactoryUtil.getLog(AgentEntryPersistenceImpl.class);
	private static AgentEntry _nullAgentEntry = new AgentEntryImpl() {
			@Override
			public Object clone() {
				return this;
			}

			@Override
			public CacheModel<AgentEntry> toCacheModel() {
				return _nullAgentEntryCacheModel;
			}
		};

	private static CacheModel<AgentEntry> _nullAgentEntryCacheModel = new CacheModel<AgentEntry>() {
			public AgentEntry toEntityModel() {
				return _nullAgentEntry;
			}
		};
}